﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ChangeProblem.Logic
{
   public  class OfficeServices
    {
        TodoData todoData;

        public OfficeServices()
        {
            todoData = new TodoData();
        }
        /// <summary>
        /// 创建用户清单
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public List<DetialList> CreatList(DetialList list)
        {
            var user = todoData.users.Find(u => u.UserId == list.UserId);
            user.Lists.Add(list);
            todoData.detialLists.Add(list);
            return todoData.detialLists;
        }
        /// <summary>
        /// 修改清单名字
        /// </summary>
        /// <param name="detialId"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        public DetialList ChangeDetialName(int detialId,string name)
        {
            var detialList = todoData.detialLists.Find(d => d.ListId == detialId);
            detialList.Name = name;
            return detialList;
        }
        /// <summary>
        /// 创建待办事项
        /// </summary>
        /// <param name="matter"></param>
        /// <returns></returns>
        public List<Matter> CreatMatter(Matter matter)
        {
            var list = todoData.detialLists.Find(l => l.UserId == matter.UserId);
            matter.Stage = TodoData.Stage.unover.ToString();
            list.Matters.Add(matter);
            todoData.matters.Add(matter);
            return todoData.matters;
        }
        /// <summary>
        /// 添加备注
        /// </summary>
        /// <param name="matterId"></param>
        /// <param name="remarks"></param>
        /// <returns></returns>
        public Matter AddRemarks(int matterId,string remarks)
        {
            var matter = todoData.matters.Find(m => m.MatterId == matterId);
            matter.Remarks = remarks;
            return matter;
        }
        /// <summary>
        /// 完成事项
        /// </summary>
        /// <param name="matterId"></param>
        /// <returns></returns>
        public Matter OverMatter(int matterId)
        {
            var matter = todoData.matters.Find(m => m.MatterId == matterId);
            matter.Stage = TodoData.Stage.over.ToString();
            return matter;
        }
        /// <summary>
        /// 取消
        /// </summary>
        /// <param name="matterId"></param>
        /// <returns></returns>
        public Matter CancelMatter(int matterId)
        {
            var matter = todoData.matters.Find(m => m.MatterId == matterId);
            matter.Stage = TodoData.Stage.cancel.ToString();
            return matter;
        }
        /// <summary>
        /// 添加过期时间
        /// </summary>
        /// <param name="matterId"></param>
        /// <param name="overduetime"></param>
        /// <returns></returns>
        public Matter AddOverduetime(int matterId,DateTime overduetime)
        {
            var matter = todoData.matters.Find(m => m.MatterId == matterId);
            matter.OverdueTime = overduetime;
            return matter;
        }
        public List<Matter> DeleteMatter(int matterId)
        {
            var matter = todoData.matters.Find(m => m.MatterId == matterId);
            todoData.matters.Remove(matter);
            return todoData.matters;
        }
        /// <summary>
        /// 自动进入过期状态
        /// </summary>
        /// <param name="time"></param>
        /// <param name="matter"></param>
        /// <returns></returns>
        public Matter ChangeToPasstime(DateTime time,Matter matter)
        {
            if (DateTime.Compare(time, matter.OverdueTime) < 0)
            {
                matter.Stage = TodoData.Stage.passtime.ToString();
            }
            return matter;
        }
        /// <summary>
         /// 用户查看所有清单
         /// </summary>
         /// <param name="UserId"></param>
         /// <returns></returns>
        public List<DetialList> LookAllLists(int userId)
        {
            return todoData.detialLists.Where(l => l.UserId == userId).ToList();
        }
        /// <summary>
        /// 用户查看某个清单的所有事项
        /// </summary>
        /// <param name="detialId"></param>
        /// <returns></returns>
        public List<Matter> LookMatterByDetial(int detialId)
        {
            return todoData.matters.Where(m => m.DetialListId == detialId).ToList();
        }
        /// <summary>
        /// 用户未完成事项统计
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public int NumOfUnover(int userId)
        {
            return todoData.matters.Where(m => m.Stage.Equals(TodoData.Stage.unover.ToString())).Count();
        }
        /// <summary>
        /// 用户所有事项查看
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public List<Matter> LookAllMatter(int userId)
        {
            return todoData.matters.Where(m => m.UserId == userId).ToList();
        }


    }
}
